package com.hqyj.javaSpringCloudAccount.controller;

import com.github.pagehelper.PageInfo;

import com.hqyj.entity.account.User;
import com.hqyj.entity.common.Result;
import com.hqyj.entity.common.SearchBean;
import com.hqyj.javaSpringCloudAccount.entity.UserVo;
import com.hqyj.javaSpringCloudAccount.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/account/")
public class UserController {

    @Autowired
    public UserService userService;

    /**
     * 用户登录接口
     * 127.0.0.1:8080/api/login ---- post
     * {"userName":"zhangsan","password":"123456"}
     */
    @PostMapping(value = "/login", consumes = "application/json")
    public Result<User> login(@RequestBody User user){
        return userService.login(user);
    }

    /**
     * 通过 id 获取用户信息接口
     * 127.0.0.1:8080/api/user/{id} ---- get
     */
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable int id){
        return userService.getUserById(id);
    }

    /**
     * 添加用户接口
     * 127.0.0.1:8080/api/user ---- post
     * 仅测试添加用户：{"userName":"gyc","email":"gyc@163.com","password":"123456"}
     * 测试 UserRole:
     *  {"userName":"zhangsanfeng1","email":"zsf1@163.com","password":"123456","roles":[{"id": 1, "roleName": "admin"}, {"id": 2, "roleName": "manager"}]}
     */
    @PostMapping(value = "/user", consumes = "application/json")
    public Result<User> insertUser(@RequestBody User user){
        return userService.insertUser(user);
    }

    /**
     * 更新用户信息接口
     * 127.0.0.1:8080/api/user ---- put
     * 仅更新用户信息：{"id":1,"userName":"zhangsanfeng","email":"zsf@163.com","password":"123"}
     * 更新用户及角色信息:
     * {"id":1,"userName":"zhangsanfeng","email":"zsf@163.com","password":"123","roles":[{"id": 1, "roleName": "admin"}, {"id": 2, "roleName": "manager"}]}
     */
    @PutMapping(value = "/user",consumes = "application/json")
    public Result<User> updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    /**
     * 通过 id 删除用户接口
     * 127.0.0.1:8080/api/user/{id} ---- delete
     */
    @DeleteMapping("/user/{id}")
//    @RequiresPermissions("/api/user/{id}") // 此处500
    public Result<Object> deleteUserById(@PathVariable int id){
        return userService.deleteUserById(id);
    }

    /**
     * users 的多条件分页查询接口
     * 127.0.0.1:8080/api/users ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"id","direction":"asc","keyword":""}
     */
    @PostMapping(value = "/users", consumes = "application/json")
    public PageInfo<User> getUsersBySearchBean(@RequestBody SearchBean searchBean) {
        return userService.getUsersBySearchBean(searchBean);
    }

    /**
     * 127.0.0.1/api/users/excel?keyWord=hj ---- get
     */
    @GetMapping(value = "/users/excel")
    public void exportExcel(@RequestParam String keyWord,
                            HttpServletResponse response) throws IOException {
        List<User> users = userService.getUsersByKeyWord(keyWord);
    }

    /**
     * 127.0.0.1:8762/api/account/userVo/10 --- get
     * @param id
     * @return
     */
    @GetMapping("/userVo/{id}")
    public UserVo getUserVoById(@PathVariable int id) {
        return userService.getUserVoById(id);
    }
}
